<?php
class ManageController extends Zend_Controller_Action
{
    /**
     * 进入控制器的时候查看客户端是否存在cookie
     * 如果存在验证通过直接分配session
     * 否则走到客户端指定的控制器
     * @see Zend_Controller_Action::init()
     */
    public function init()
    {
    	parent::init();
    	
    	$actionName = $this->_request->getActionName();
    	$notCheck = array('index', 'login', 'logout');    	
        $cookieName = Zend_Registry::get('data')->admin->cookie;
        if (isset($_COOKIE[$cookieName])) {
            $authObj = new Business_Manage_Auth();
            $cookieResult = $authObj->validateAuthSignature(
                $_COOKIE[$cookieName]
            );
            if ($cookieResult['errorcode'] == 0) {
                $authObj->setUserLogin($cookieResult['result']);
            }
        }
        if (!in_array($actionName, $notCheck)) {
        	$authObj = new Business_Manage_Auth();
        	if (!$authObj->isLogin()) {
        		$this->_redirect('/manage/index');
        	}
        }
    }
    
    /**
     * 管理员登陆界面
     * 如果登陆直接进入homepage管理页
     */
    public function indexAction()
    {
        $this->view->title = '霆嘉科技管理员登录';
        $authObj = new Business_Manage_Auth();
        if ($authObj->isLogin()) {
            $this->_redirect('/manage/homepage');
        }
    }
    
    /**
     * 用户登陆验证action
     * 如果用户名密码正确直接分配session
     * 如果有选择记住我，就分配cookie
     * 成功之后跳转到homepage管理页
     */
    public function loginAction()
    {
        $username = $this->_request->getParam('username');
        $password = $this->_request->getParam('password');
        $rememberme = $this->_request->getParam('rememberme');
        if (isset($username) && isset($password) && $username != "" &&
         $password != "") {
            $authObj = new Business_Manage_Auth();
            $authResult = $authObj->checkLogin($username, $password);
            if ($authResult['errorcode'] == 0) {
                if (isset($rememberme)) {
                    $cookieName = Zend_Registry::get('data')->admin->cookie;
                    $cookie = $authObj->generateAuthSignature(
                        $authResult['result'], 
                        $authObj->getCookieExpireTime()
                    );
                    setcookie(
                        $cookieName,
                        $cookie, 
                        $authObj->getCookieExpireTime()
                    );
                }
                $authObj->setUserLogin($authResult['result']);
                $this->_redirect('/manage/homepage');
            } else {
                $this->view->errormsg = '<div id="login_error">登录失败！</div>';
                $this->render('index');
            }
        } else {
            $this->view->errormsg = '<div id="login_error">登录失败！</div>';
            $this->render('index');
        }
    }
    
    /**
     * homepage管理页面
     * 包括登陆验证
     */
    public function homepageAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'category';
    	
        $authObj = new Business_Manage_Auth();
        $view->title = '霆嘉科技管理网站首页';
        $category = new Business_Category();
        $view->categories = $categories = $category->getAll();
    }
    /**
     * 添加产品分类页面
     */
    public function addProductCategoryAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'category';
    }
    /**
     * 添加产品分类动作
     */
    public function addProductCategoryDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'category';
    	$view->backUrl = '/manage/homepage';
    	$view->title = "产品分类添加结果";
    	
    	$cate = $this->_request->getParam('cate', '');
    	
    	if ($cate != '') {
    		$category = new Business_Category();
    		$rtn = $category->add($cate);
    		if ($rtn) {
    			$view->errorcode = 0;
    			$view->errormsg = "添加成功！";
    		} else {
    			$view->errorcode = -1;
    			$view->errormsg = "插入数据库出错！";
    		}
    	} else {
    		$view->errorcode = -2;
    		$view->errormsg = "分类名称不能为空！";
    	}
    	$this->render("action-result");
    }
    
    /**
     * 编辑产品分类页面
     */
    public function editProductCategoryAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'category';
    	
    	$cateId = $this->_request->getParam('cateid', 0);
    	$category = new Business_Category();
    	$view->cate = $category->getById($cateId);
    }
    
    public function editProductCategoryDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'category';
    	$view->backUrl = '/manage/homepage';
    	$view->title = "产品分类更新结果";
    	
    	$cateId = $this->_request->getParam('cateId');
    	$cateName = $this->_request->getParam('cate');
    	
    	$category = new Business_Category();
    	$affectedRows = $category->update($cateName, $cateId);
    	if ($affectedRows) {
    		$view->errorcode = 0;
    		$view->errormsg = "更新成功！";
    	} else {
    		$view->errorcode = -1;
    		$view->errormsg = "更新失败！";
    	}
    	$this->render("action-result");
    }
    
    /**
     * 删除产品分类
     */
    public function deleteProductCategoryAction()
    {
		$cateId = $this->_request->getParam('cateid');
    	$category = new Business_Category();
    	$delRows = $category->delete($cateId);
    	$result = array();
    	if ($delRows) {
    		$result['errorcode'] = 0;
    		$result['errormsg'] = "删除成功！";
    	} else {
    		$result['errorcode'] = -1;
    		$result['errormsg'] = "删除失败！";
    	}
    	$this->_helper->getHelper('Json')->sendJson($result);
    }
    
    public function productAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'product';
    	$view->title = '产品设备管理';
    	$cateId = $this->_request->getParam('cateid' , '');
    	
    	$category = new Business_Category();
    	$view->cates = $cates = $category->getAll();
    	
    	if ($cateId == '') {
    		if (isset($cates[0]) && isset($cates[0]['id'])) {
    			$cateId = $cates['0']['id'];
    		}
    	}
    	$view->cateId = $cateId;
    	
    	$product = new Business_Product();
    	//当前页码
    	$view->page = $page = $this->_request->getParam('page', 1);
    	//记录总数
    	$amount = $product->getByCateIdAmount($cateId);
    	$view->amount = $amount;
    	//每页显示
    	$rowCount = Zend_Registry::get('data')->product->numPerPage;
    	$view->rowCount = $rowCount;
    	//总页数
    	$view->total = $total = ceil($amount / $rowCount);
    	//页码超过范围处理
    	if ($page < 1) {
    		$view->page = $page = 1;
    	}
    	if ($page > $total) {
    		$view->page = $page = $total;
    	}
    	$view->productList = $productList = $product->getByCateId($cateId, $page, $rowCount);
    }
    
    public function addProductAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'product';
    	$view->title = "添加产品";
    	
    	$category = new Business_Category();
    	$view->categories = $category->getAll();
    }
    
    public function addProductDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'product';
    	$view->backUrl = '/manage/product';
    	$view->title = "产品添加结果";
    	
    	$name = $this->_request->getParam('name', '');
    	$cover = $this->_request->getParam('cover', '');
    	$cate = $this->_request->getParam('cate', '');
    	$isshow = $this->_request->getParam('isshow', '');
    	$content = $this->_request->getParam('content', '');
    	$isshow = $isshow == 'on' ? 1 : 0;
    	$data = array(
    		'name' => $name,
    		'cover' => $cover,
    		'introduce' => $content,
    		'cateid' => $cate,
    		'show_homepage' => $isshow
    	);
    	
    	$product = new Business_Product();
    	$insRow = $product->add($data);
    	if ($insRow) {
    		$view->errorcode = 0;
    		$view->errormsg = "添加成功！";
    	} else {
    		$view->errorcode = -1;
    		$view->errormsg = "添加失败！";
    	}
    	$this->render("action-result");
    }
    
    public function editProductAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'product';
    	$view->title = "产品添加结果";
    	
    	$pid = $this->_request->getParam("pid", 0);
    	$product = new Business_Product();
    	$view->product = $product->getById($pid);
    	
    	$category = new Business_Category();
    	$view->categories = $category->getAll();
    }
    
    public function editProductDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'product';
    	$view->level2 = 'product';
    	$view->backUrl = '/manage/product';
    	$view->title = "产品更新结果";
    	
    	$pid = $this->_request->getParam('pid', 0);
    	$name = $this->_request->getParam('name', '');
    	$cover = $this->_request->getParam('cover', '');
    	$cate = $this->_request->getParam('cate', '');
    	$isshow = $this->_request->getParam('isshow', '');
    	$content = $this->_request->getParam('content', '');
    	$isshow = $isshow == 'on' ? 1 : 0;
    	$data = array(
    		'name' => $name,
    		'cover' => $cover,
    		'introduce' => $content,
    		'cateid' => $cate,
   			'show_homepage' => $isshow
    	);
    	 
    	$product = new Business_Product();
    	$insRow = $product->update($data, $pid);
    	if ($insRow) {
    		$view->errorcode = 0;
    		$view->errormsg = "更新成功！";
    	} else {
    		$view->errorcode = -1;
    		$view->errormsg = "更新失败！";
    	}
    	$this->render("action-result");
    }
    
    /**
     * 删除产品
     */
    public function deleteProductAction()
    {
    	$pid = $this->_request->getParam('pid');
    	$product = new Business_Product();
    	$delRows = $product->delete($pid);
    	$result = array();
    	if ($delRows) {
    		$result['errorcode'] = 0;
    		$result['errormsg'] = "删除成功！";
    	} else {
    		$result['errorcode'] = -1;
    		$result['errormsg'] = "删除失败！";
    	}
    	$this->_helper->getHelper('Json')->sendJson($result);
    }
    
    /**
     * 用户退出action
     * 退出之后跳转到首页
     */
    public function logoutAction()
    {
        $authObj = new Business_Manage_Auth();
        $authObj->logout();
        $authObj->deleteCookie();
        $this->_redirect('/manage');
    }
    
    /**
     * 模版完整页面
     */
    public function newAction()
    {
    	
    }
    
    /**
     * 新闻分类展示页面
     */
    public function newsCategoryAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'category';
    	 
    	$authObj = new Business_Manage_Auth();
    	$view->title = '霆嘉科技-新闻分类管理';
    	$category = new Business_Category();
    	$category->setType('news');
    	$view->categories = $categories = $category->getAll();
    }
    
    /**
     * 添加新闻分类页面
     */
    public function addNewsCategoryAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'category';
    }
    /**
     * 添加新闻分类动作
     */
    public function addNewsCategoryDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'category';
    	$view->backUrl = '/manage/news-category';
    	$view->title = "新闻分类添加结果";
    	 
    	$cate = $this->_request->getParam('cate', '');
    	 
    	if ($cate != '') {
    		$category = new Business_Category();
    		$category->setType('news');
    		$rtn = $category->add($cate);
    		if ($rtn) {
    			$view->errorcode = 0;
    			$view->errormsg = "添加成功！";
    		} else {
    			$view->errorcode = -1;
    			$view->errormsg = "插入数据库出错！";
    		}
    	} else {
    		$view->errorcode = -2;
    		$view->errormsg = "分类名称不能为空！";
    	}
    	$this->render("action-result");
    }
    
    /**
     * 编辑新闻分类页面
     */
    public function editNewsCategoryAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'category';
    	 
    	$cateId = $this->_request->getParam('cateid', 0);
    	$category = new Business_Category();
    	$category->setType('news');
    	$view->cate = $category->getById($cateId);
    }
    
    /**
     * 编辑新闻分类操作
     */
    public function editNewsCategoryDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'category';
    	$view->backUrl = '/manage/news-category';
    	$view->title = "新闻分类更新结果";
    	 
    	$cateId = $this->_request->getParam('cateId');
    	$cateName = $this->_request->getParam('cate');
    	 
    	$category = new Business_Category();
    	$category->setType('news');
    	$affectedRows = $category->update($cateName, $cateId);
    	if ($affectedRows) {
    		$view->errorcode = 0;
    		$view->errormsg = "更新成功！";
    	} else {
    		$view->errorcode = -1;
    		$view->errormsg = "更新失败！";
    	}
    	$this->render("action-result");
    }
    
    /**
     * 删除新闻分类
     */
    public function deleteNewsCategoryAction()
    {
    	$cateId = $this->_request->getParam('cateid');
    	$category = new Business_Category();
    	$category->setType('news');
    	$delRows = $category->delete($cateId);
    	$result = array();
    	if ($delRows) {
    		$result['errorcode'] = 0;
    		$result['errormsg'] = "删除成功！";
    	} else {
    		$result['errorcode'] = -1;
    		$result['errormsg'] = "删除失败！";
    	}
    	$this->_helper->getHelper('Json')->sendJson($result);
    }
    
	public function newsAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'news';
    	$view->title = '新闻管理-新闻管理';
    	$cateId = $this->_request->getParam('cateid' , '');
    	
    	$category = new Business_Category();
    	$category->setType('news');
    	$view->cates = $cates = $category->getAll();
    	
    	if ($cateId == '') {
    		if (isset($cates[0]) && isset($cates[0]['id'])) {
    			$cateId = $cates['0']['id'];
    		}
    	}
    	$view->cateId = $cateId;
    	
    	$product = new Business_News();
    	//当前页码
    	$view->page = $page = $this->_request->getParam('page', 1);
    	//记录总数
    	$amount = $product->getByCateIdAmount($cateId);
    	$view->amount = $amount;
    	//每页显示
    	$rowCount = Zend_Registry::get('data')->product->numPerPage;
    	$view->rowCount = $rowCount;
    	//总页数
    	$view->total = $total = ceil($amount / $rowCount);
    	//页码超过范围处理
    	if ($page < 1) {
    		$view->page = $page = 1;
    	}
    	if ($page > $total) {
    		$view->page = $page = $total;
    	}
    	$view->newsList = $productList = $product->getByCateId($cateId, $page, $rowCount);
    }
    
    public function addNewsAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'news';
    	$view->title = "新闻管理-添加新闻";
    	 
    	$category = new Business_Category();
    	$category->setType('news');
    	$view->categories = $category->getAll();
    }
    
    public function addNewsDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'news';
    	$view->backUrl = '/manage/news';
    	$view->title = "新闻管理-添加结果";
    	 
    	$title = $this->_request->getParam('title', '');
    	$content = $this->_request->getParam('content', '');
    	$pubtime = $this->_request->getParam('pubtime', date("Y-m-d H:i:s"));
    	$cate = $this->_request->getParam('cate', '');
    	$data = array(
    			'title' => $title,
    			'content' => $content,
    			'pubtime' => $pubtime,
    			'cateid' => $cate
    	);
    	 
    	$product = new Business_News();
    	$insRow = $product->add($data);
    	if ($insRow) {
    		$view->errorcode = 0;
    		$view->errormsg = "添加成功！";
    	} else {
    		$view->errorcode = -1;
    		$view->errormsg = "添加失败！";
    	}
    	$this->render("action-result");
    }
    
    public function editNewsAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'news';
    	$view->title = "新闻管理-编辑新闻";
    	 
    	$id = $this->_request->getParam("id", 0);
    	$news = new Business_News();
    	$view->news = $news->getById($id);
    	 
    	$category = new Business_Category();
    	$category->setType('news');
    	$view->categories = $category->getAll();
    }
    
    public function editNewsDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'news';
    	$view->level2 = 'news';
    	$view->backUrl = '/manage/news';
    	$view->title = "新闻管理-更新结果";
    	 
    	$id = $this->_request->getParam('id', 0);
    	$title = $this->_request->getParam('title', '');
    	$content = $this->_request->getParam('content', '');
    	$pubtime = $this->_request->getParam('pubtime', '');
    	$cateid = $this->_request->getParam('cate', '');
    	$data = array(
    		'title' => $title,
    		'content' => $content,
    		'pubtime' => $pubtime,
    		'cateid' => $cateid,
    	);
    
    	$product = new Business_News();
    	$insRow = $product->update($data, $id);
    	if ($insRow) {
    		$view->errorcode = 0;
    		$view->errormsg = "更新成功！";
    	} else {
    		$view->errorcode = -1;
    		$view->errormsg = "更新失败！";
    	}
    	$this->render("action-result");
    }
    
    /**
     * 删除新闻
     */
    public function deleteNewsAction()
    {
    	$id = $this->_request->getParam('id');
    	$product = new Business_News();
    	$delRows = $product->delete($id);
    	$result = array();
    	if ($delRows) {
    		$result['errorcode'] = 0;
    		$result['errormsg'] = "删除成功！";
    	} else {
    		$result['errorcode'] = -1;
    		$result['errormsg'] = "删除失败！";
    	}
    	$this->_helper->getHelper('Json')->sendJson($result);
    }
    
	public function infoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'info';
    	$view->level2 = 'info';
    	$view->title = '信息管理-公用信息管理';
    	$cateId = $this->_request->getParam('cateid' , '');
    	
    	$category = new Business_Category();
    	$category->setType('post');
    	$view->cates = $cates = $category->getAll();
    	
    	if ($cateId == '') {
    		if (isset($cates[0]) && isset($cates[0]['id'])) {
    			$cateId = $cates['0']['id'];
    		}
    	}
    	$view->cateId = $cateId;
    	
    	$product = new Business_Post();
    	//当前页码
    	$view->page = $page = $this->_request->getParam('page', 1);
    	//记录总数
    	$amount = $product->getByCateIdAmount($cateId);
    	$view->amount = $amount;
    	//每页显示
    	$rowCount = Zend_Registry::get('data')->product->numPerPage;
    	$view->rowCount = $rowCount;
    	//总页数
    	$view->total = $total = ceil($amount / $rowCount);
    	//页码超过范围处理
    	if ($page < 1) {
    		$view->page = $page = 1;
    	}
    	if ($page > $total) {
    		$view->page = $page = $total;
    	}
    	$view->newsList = $productList = $product->getByCateId($cateId, $page, $rowCount);
    }
    
    public function addPostAction()
    {
    	$view = $this->view;
    	$view->level1 = 'info';
    	$view->level2 = 'info';
    	$view->title = "信息管理-公用信息管理";
    
    	$category = new Business_Category();
    	$category->setType('post');
    	$view->categories = $category->getAll();
    }
    
    public function addPostDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'info';
    	$view->level2 = 'info';
    	$view->backUrl = '/manage/info';
    	$view->title = "信息管理-添加结果";
    
    	$title = $this->_request->getParam('title', '');
    	$content = $this->_request->getParam('content', '');
    	$cate = $this->_request->getParam('cate', '');
    	$data = array(
    		'title' => $title,
    		'content' => $content,
    		'cateid' => $cate
    	);
    
    	$product = new Business_Post();
    	$insRow = $product->add($data);
    	if ($insRow) {
    		$view->errorcode = 0;
    		$view->errormsg = "添加成功！";
    	} else {
    		$view->errorcode = -1;
    		$view->errormsg = "添加失败！";
    	}
    	$this->render("action-result");
    }
    
    public function editPostAction()
    {
    	$view = $this->view;
    	$view->level1 = 'info';
    	$view->level2 = 'info';
    	$view->title = "信息管理-公用信息管理";
    
    	$id = $this->_request->getParam("id", 0);
    	$news = new Business_Post();
    	$view->news = $news->getById($id);
    
    	$category = new Business_Category();
    	$category->setType('post');
    	$view->categories = $category->getAll();
    }
    
    public function editPostDoAction()
    {
    	$view = $this->view;
    	$view->level1 = 'info';
    	$view->level2 = 'info';
    	$view->backUrl = '/manage/info';
    	$view->title = "信息管理-公用信息管理";
    
    	$id = $this->_request->getParam('id', 0);
    	$title = $this->_request->getParam('title', '');
    	$content = $this->_request->getParam('content', '');
    	$cateid = $this->_request->getParam('cate', '');
    	$data = array(
    			'title' => $title,
    			'content' => $content,
    			'cateid' => $cateid,
    	);
    
    	$product = new Business_Post();
    	$insRow = $product->update($data, $id);
    	if ($insRow) {
    		$view->errorcode = 0;
    		$view->errormsg = "更新成功！";
    	} else {
    		$view->errorcode = -1;
    		$view->errormsg = "更新失败！";
    	}
    	$this->render("action-result");
    }
    
    /**
     * 删除新闻
     */
    public function deletePostAction()
    {
    	$id = $this->_request->getParam('id');
    	$product = new Business_Post();
    	$delRows = $product->delete($id);
    	$result = array();
    	if ($delRows) {
    		$result['errorcode'] = 0;
    		$result['errormsg'] = "删除成功！";
    	} else {
    		$result['errorcode'] = -1;
    		$result['errormsg'] = "删除失败！";
    	}
    	$this->_helper->getHelper('Json')->sendJson($result);
    }
    
    /**
     * 查看用户信息反馈
     */
    public function feedbackAction()
    {
    	$view = $this->view;
    	$view->level1 = 'feedback';
    	$view->level2 = 'feedback';
    	$view->title = '用户信息反馈列表';
    	
    	$business = new Business_Feedback();
    	//当前页码
    	$view->page = $page = $this->_request->getParam('page', 1);
    	//记录总数
    	$amount = $business->getAmount();
    	$view->amount = $amount;
    	//每页显示
    	$rowCount = Zend_Registry::get('data')->product->numPerPage;
    	$view->rowCount = $rowCount;
    	//总页数
    	$view->total = $total = ceil($amount / $rowCount);
    	//页码超过范围处理
    	if ($page < 1) {
    		$view->page = $page = 1;
    	}
    	if ($page > $total) {
    		$view->page = $page = $total;
    	}
    	$view->list = $list = $business->get($page, $rowCount);
    }
    
    /**
     * 删除新闻
     */
    public function deleteFeedbackAction()
    {
    	$id = $this->_request->getParam('id');
    	$product = new Business_Feedback();
    	$delRows = $product->delete($id);
    	$result = array();
    	if ($delRows) {
    		$result['errorcode'] = 0;
    		$result['errormsg'] = "删除成功！";
    	} else {
    		$result['errorcode'] = -1;
    		$result['errormsg'] = "删除失败！";
    	}
    	$this->_helper->getHelper('Json')->sendJson($result);
    }
}